/**
 * <pre>
 * Title: 		MessageServiceImpl.java
 * Project: 	itfinoss
 * Author:		pengfangliang
 * Create:	 	2012-8-10 上午09:56:53
 * Copyright: 	Copyright (c) 2012
 * Company:		Shenzhen Helper
 * <pre>
 */
package com.hengpeng.itfinoss.service.impl;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.hengpeng.common.persist.QueryResult;
import com.hengpeng.common.persist.entity.AbstractEntity.BoolValue;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.model.Order.OrderType;
import com.hengpeng.common.util.StringHelper;
import com.hengpeng.itfinbase.persist.Message;
import com.hengpeng.itfinbase.persist.Message.MessageSendType;
import com.hengpeng.itfinbase.persist.Message.MessageType;
import com.hengpeng.itfinoss.service.MessageService;
import com.hengpeng.itfinoss.service.ServiceFactory;

/**
 * <pre>
 *  站内信服务层接口实现
 * </pre>
 * @author pengfangliang
 * @version 1.0, 2012-8-10
 */
@Service
@Lazy
public class MessageServiceImpl implements MessageService
{
	@Autowired
	private ServiceFactory serviceFactory;

	@Override
	public QueryResult<Message> findMessageWithPage(String title, MessageType type, MessageSendType sendType,
			BoolValue isPublish, Date beginPublishDate, Date endPublishDate, String userName, Integer pageIndex,
			Integer pageSize, LinkedHashMap<String, OrderType> sortMap)
	{
		DynamicQuery query = new DynamicQuery(Message.class);
		query.setIsSearchTotalCount(true);
		if (!StringHelper.isEmpty(title))
		{
			query.like("title", "%" + title + "%");
		}
		query.eq("type", type);
		query.eq("sendType", sendType);
		query.eq("isPublish", isPublish);
		if (!StringUtils.isEmpty(userName))
		{
			query.eq("userName", userName);
		}
		if (beginPublishDate != null)
		{
			query.ge("publishDate", beginPublishDate);
		}
		if (endPublishDate != null)
		{
			query.le("publishDate", endPublishDate);
		}
		query.setPageIndex(pageIndex);
		query.setPageSize(pageSize);
		if (sortMap != null)
		{
			for (Entry<String, OrderType> entry : sortMap.entrySet())
			{
				if (entry.getValue() == OrderType.DESC)
				{
					query.desc(entry.getKey());
				}
				else
				{
					query.asc(entry.getKey());
				}
			}
		}
		System.out.println(query.getSqlResult().getSql());
		return serviceFactory.getPersistService().findWithPage(Message.class, query);
	}
}
